package com.yz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yz.common.CustomException;
import com.yz.common.R;
import com.yz.entity.AddressBook;
import com.yz.entity.Orders;
import com.yz.entity.User;
import com.yz.service.AddressBookService;
import com.yz.service.OrdersService;
import com.yz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 提交订单
     * @param order
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order, HttpSession session){
        log.info("Orders:{}", order);
        ordersService.saveSelf(order,session);
        return R.success("支付成功");
    }

    /**
     * 获取用户订单分页
     * @param page
     * @param pageSize
     * @param session
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(int page,int pageSize,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,userId);
        ordersService.page(ordersPage,wrapper);
        return R.success(ordersPage);
    }
}
